package com.xhu.collegestudentrentalsystem.module.manager.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xhu.collegestudentrentalsystem.annocation.LoginRequired;
import com.xhu.collegestudentrentalsystem.module.manager.service.impl.ManagerServiceImpl;
import com.xhu.collegestudentrentalsystem.module.user.entity.House;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.module.user.entity.User;
import com.xhu.collegestudentrentalsystem.module.user.service.impl.HouseServiceImpl;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import com.xhu.collegestudentrentalsystem.util.constant.StatusMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/29 15:16
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerServiceImpl managerService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private HouseServiceImpl houseService;

    /**
     * 获取用户列表
     */
    @LoginRequired
    @RequestMapping("/userList")
    public String userList(Page page, Model model){
        page.setRows(5);
        List<User> users = managerService.findAllUser(page);
        model.addAttribute(KeyMsg.user.USER_LIST, users);
        page.setPath("/manager/userList");
        return "site/user-list";
    }

    /**
     * 更改用户状态
     */
    @RequestMapping("/changeUserStatus")
    @ResponseBody
    public ResultData changeUserStatus(Long userId){
        if(hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        Integer result = managerService.updateUserStatus(userId);
        if(result > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.modify.MODIFY_SUCCESS);
        }else {
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.modify.MODIFY_FAIL);
        }
    }

    /**
     * 管理员查看房源信息
     */
    @LoginRequired
    @RequestMapping("/housesManage")
    public String housesManage(Page page, Model model){
        // 查询所有的房源信息
        page.setLimit(9);
        List<House> houses = managerService.findAllHouses(page);
        model.addAttribute(KeyMsg.house.HOUSE_LIST, houses);
        model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_SUCCESS);
        page.setPath("/manager/housesManage");
        return "site/owner-house";
    }

    /**
     * 管理员审核通过房屋
     */
    @LoginRequired
    @RequestMapping("/passAudit")
    public String passAudit(Long houseId, Page page, Model model){
        Integer result = managerService.passHouse(houseId);
        if(result > 0){
            model.addAttribute(KeyMsg.MSG, PointMsg.operation.OPERATION_SUCCESS);
        }else {
            model.addAttribute(KeyMsg.MSG, PointMsg.operation.OPERATION_FAIL);
        }
        return housesManage(page, model);
    }

    /**
     * 管理员审核未通过
     */
    @LoginRequired
    @RequestMapping("/unPassAudit")
    public String unPassAudit(Long houseId, Page page, Model model){
        Integer result = managerService.unPassHouse(houseId);
        if(result > 0){
            model.addAttribute(KeyMsg.MSG, PointMsg.operation.OPERATION_SUCCESS);
        }else {
            model.addAttribute(KeyMsg.MSG, PointMsg.operation.OPERATION_FAIL);
        }
        return housesManage(page, model);
    }

    /**
     * 管理员上架房子
     */
    @LoginRequired
    @RequestMapping("/upHouse")
    public String managerUpHouse(Page page, Long houseId, Model model){
        // 查询是否有该房子信息
        House house = houseService.findHouseById(houseId);
        if(house == null){
            model.addAttribute(KeyMsg.ERROR_MSG, PointMsg.house.HOUSE_INFO_NOT_FOUND);
            return "error/500";
        }

        // 修改房子状态为未出租
        Integer result = houseService.updateHouseStatus(houseId, StatusMsg.houseStatus.UN_RENT);
        if(result > 0){
            model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_SUCCESS);
            model.addAttribute(KeyMsg.MSG, PointMsg.house.UP_SUCCESS);
        }else {
            model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_FAIL);
            model.addAttribute(KeyMsg.MSG, PointMsg.house.UP_FAIL);
        }
        return housesManage(page, model);
    }

    /**
     * 管理员下架房子
     */
    @LoginRequired
    @RequestMapping("/downHouse")
    public String downHouse(Page page, Long houseId, Model model){
        // 查询是否有该房子信息
        House house = houseService.findHouseById(houseId);
        if(house == null){
            model.addAttribute(KeyMsg.ERROR_MSG, PointMsg.house.HOUSE_INFO_NOT_FOUND);
            return "error/500";
        }

        if(house.getStatus() == StatusMsg.houseStatus.HAS_RENT){
            model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_FAIL);
            model.addAttribute(KeyMsg.MSG, PointMsg.house.HOUSE_HAS_RENT);
            return housesManage(page, model);
        }

        // 修改房子状态为下架状态
        Integer result = houseService.updateHouseStatus(houseId, StatusMsg.houseStatus.HAS_DOWN);
        if(result > 0){
            model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_SUCCESS);
            model.addAttribute(KeyMsg.MSG, PointMsg.house.DOWN_SUCCESS);
        }else {
            model.addAttribute(KeyMsg.CODE, ResultData.OPERATION_FAIL);
            model.addAttribute(KeyMsg.MSG, PointMsg.house.DOWN_FAIL);
        }
        return housesManage(page, model);
    }



}
